package com.yanqu.road.dao.impl.servercenter;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 平台道具配置
 */
public class PlatformGoodsDaoImpl extends TempDao {

	public Map<Integer, GoodsInfo> getGoodsInfoMap(int channelId) {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_s_platform_goods where channelId in (?,0)";
		Map<Integer, GoodsInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getServerDb());
			call = getDb().getConnection().prepareCall(sql);
			call.setInt(1, channelId);
			rs = call.executeQuery();
			while (rs.next()) {
				GoodsInfo data = ResultSetParseEntityManger.initGoodsInfo(rs);
				String value = rs.getString("mailTitle");
				if(!StringUtils.isNullOrEmpty(value)){
					data.setMailTitle(value);
				}
				value = rs.getString("mailContent");
				if(!StringUtils.isNullOrEmpty(value)){
					data.setMailContent(value);
				}
				dataMap.put(data.getGoodsId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
